package cn.ittaoli.ajax.controller;

import cn.ittaoli.ajax.model.Nav;
import cn.ittaoli.ajax.service.NavService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * Created by lub_lenovo on 2017/8/8.
 */
@Controller
@RequestMapping("/nav")
public class NavController {
    @Autowired
    private NavService navService;

    @RequestMapping("/navList")
    public String navList(Model model,@ModelAttribute("msg") String msg){
        List<Nav> navList=navService.getAll();
        model.addAttribute("navList",navList);
        model.addAttribute("menu","导航列表");
        model.addAttribute("msg",msg);
        return "nav/navList";
    }

    @RequestMapping("/navAdd")
    public String navAdd(Model model){
        model.addAttribute("menu","新增导航");
        return "nav/navAdd";
    }

    @RequestMapping("/insertNav")
    public String insertNav(Model model, Nav nav, RedirectAttributes attr){
        Integer res=navService.insertNav(nav);
        if(res>0){
            attr.addAttribute("msg","添加成功");
            return "redirect:navList.do";
        }else{

            return "nav/navAdd";
        }
    }
}
